<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<meta http-equiv="Pragma" content="no-cache">
	<meta http-equiv="Cache-Control" content="no-cache">
	<meta http-equiv="Expires" content="0">
	<title>登录</title>
	<link href="css/login.css" type="text/css" rel="stylesheet">
</head>

<body>
	<div id="float_light" class="float_light" onmouseover="window.clearInterval(timer)" onmouseout="timer=window.setInterval('changePos()',delay);"></div>

	<div class="login">
		<div class="message">灯控后台管理系统
		</div>
		<div id="darkbannerwrap"></div>

		<form id="login-form" method="post" onsubmit="return false;">
			<input id="username" name="username" placeholder="用户名" type="text" autocomplete="off">
			<hr class="hr15">
			<input id="password" name="password" placeholder="密码" type="password" autocomplete="off">
			<hr class="hr15">
			<button style="width: 100%;" type="submit" onclick="login(this)">登录</button>
			<hr class="hr20">
			<span id="info" style="color: red"></span>
		</form>
	</div>

</body>
<script src="js/libs/jquery-2.1.1.min.js"></script>
<script type="text/javascript">
	if (top != self) {
		parent.location.href = '/login.html';
	}

	var token = localStorage.getItem("token");
	if (token != null && token.trim().length !== 0) {
		$.ajax({
			type: 'get',
			url: '/users/current?token=' + token,
			success: function(data) {
				location.href = '/index.html';
			},
			error: function(xhr, textStatus, errorThrown) {
				var msg = xhr.responseText;
				var response = JSON.parse(msg);
				var code = response.code;
				var message = response.message;
				if (code === 401) {
					localStorage.removeItem("token");
				}
			}
		});
	}

	function login(obj) {
		$(obj).attr("disabled", true);

		var username = $.trim($('#username').val());
		var password = $.trim($('#password').val());
		if (username === "" || password === "") {
			$("#info").html('用户名或者密码不能为空');
			$(obj).attr("disabled", false);
		} else {
			$.ajax({
				type: 'post',
				url: '/login',
				data: $("#login-form").serialize(),
				success: function(data) {
					localStorage.setItem("token", data.token);
					location.href = '/index.html';
				},
				error: function(xhr, textStatus, errorThrown) {
					var msg = xhr.responseText;
					var response = JSON.parse(msg);
					$("#info").html(response.message);
					$(obj).attr("disabled", false);
				}
			});

		}
	}

	var obj = document.getElementById("float_light");
	var clientHeight = 200;
	var clientWidth = 200;
	var timer;
	var delay = 80; //移动速度
	function randomNum(size) {
		return Math.floor(Math.random() * (size - 160 + 1));
	}
	obj.style.top = randomNum(clientHeight) + "px";
	var y = parseInt(obj.style.top);
	var flagY = true;

	function moveY() {
		if (flagY) {
			y++;
			if (y > clientHeight - 160) {
				flagY = false;
			}
		} else {
			y--;
			if (y < 0) {
				flagY = true;
			}
		}
		obj.style.top = y + "px";
	}
	window.οnresize = function() {
		clientHeight = document.documentElement.clientHeight;
		clientWidth = document.documentElement.clientWidth;
	}

	function changePos() {
		moveY();
	}

	function creatTimer() {
		timer = window.setInterval("changePos();", delay);
	}

	function hide() {
		obj.style.display = "none";
		obj.onmouseout = function() {
			obj.style.visibility = "hidden";
		}
	}
	var url = window.location.href;
	if (true) {
		creatTimer();
	} else {
		hide();
	} // url == "http://www.xxxxx.cn/"
</script>

</html>
